<template>
    <div>

        <el-form :inline="true" style="margin: 0px">
            <el-form-item>
                <el-button type="primary" @click="addDialogForm">新增</el-button>
            </el-form-item>
        </el-form>


        <el-table
                :data="tableData"
                style="width: 100%;margin-bottom: 20px;"
                row-key="id"
                border
                default-expand-all
                :tree-props="{children: 'children', hasChildren: 'hasChildren'}">
            <el-table-column
                    prop="date"
                    label="日期"
                    sortable
                    width="180">
            </el-table-column>
            <el-table-column
                    prop="name"
                    label="姓名"
                    sortable
                    width="180">

            </el-table-column>
            <el-table-column
                    prop="perms"
                    label="权限编码"
                    sortable>
            </el-table-column>
            <el-table-column
                    prop="category"
                    label="类型">
                <template slot-scope="scope">
                    <el-tag size="small" v-if="scope.row.type === 0">目录</el-tag>
                    <el-tag size="small" v-else-if="scope.row.type === 1" type="success">菜单</el-tag>
                    <el-tag size="small" v-else-if="scope.row.type === 2" type="info">按钮</el-tag>

                </template>
            </el-table-column>
            <el-table-column
                    prop="address"
                    label="地址">
            </el-table-column>
            <el-table-column
                    prop="path"
                    label="菜单URL">
            </el-table-column>
            <el-table-column
                    prop="component"
                    label="菜单组件">
            </el-table-column>
            <el-table-column
                    prop="orderNum"
                    label="排序号">
            </el-table-column>
            <el-table-column
                    prop="state"
                    label="状态">
                <template slot-scope="scope">
                    <el-tag size="small" v-if="scope.row.statu === 1" type="success">正常</el-tag>
                    <el-tag size="small" v-else-if="scope.row.statu === 0" type="danger">禁用</el-tag>

                </template>
            </el-table-column>
            <el-table-column
                    prop="address"
                    label="操作">
                <template slot-scope="scope">
                    <el-button type="text">编辑</el-button>
                    <el-divider direction="vertical" ></el-divider>
                    <template>
                        <el-popconfirm
                                title="这是一段内容确定删除吗？" @confirm="handleDelete(scope.row.id)"
                        >
                            <el-button type="text" slot="reference">删除</el-button>
                        </el-popconfirm>
                    </template>
                </template>
            </el-table-column>
        </el-table>

<!--        新增对话框-->
        <el-dialog
                title="提示"
                :visible.sync="dialogFormVisible"
                width="30%"
                :before-close="handleClose">

            <el-form :model="editForm" :rules="editFormRules " ref="editForm" label-width="100px" class="demo-editForm">
                <el-form-item label="上级菜单" prop="parentId">
                    <el-select v-model="editForm.parentId" placeholder="请选择上级菜单">
                        <template v-for="item in tableData">
                            <el-option :label="item.name" :value="item.id"></el-option>
                            <template v-for="child in item.children">
                                <el-option :label="child.name" :value="child.id">
                                    <span>{{"- " + child.name}}</span>
                                </el-option>
                            </template>
                        </template>

                    </el-select>

                </el-form-item>
                <el-form-item label="菜单名称" prop="name">
                    <el-input v-model="editForm.name"></el-input>
                </el-form-item>
                <el-form-item label="权限编码" prop="perms">
                    <el-input v-model="editForm.perms"></el-input>
                </el-form-item>
                <el-form-item label="菜单URL" prop="path">
                    <el-input v-model="editForm.path"></el-input>
                </el-form-item>
                <el-form-item label="地址" prop="address">
                    <el-input v-model="editForm.address"></el-input>
                </el-form-item>
                <el-form-item label="菜单组件" prop="component">
                    <el-input v-model="editForm.component"></el-input>
                </el-form-item>
                <el-form-item label="类型" prop="type">
                    <el-radio-group v-model="editForm.type">
                        <el-radio :label=0>目录</el-radio>
                        <el-radio :label=1>菜单</el-radio>
                        <el-radio :label=2>按钮</el-radio>
                    </el-radio-group>
                </el-form-item>
                <el-form-item label="状态" prop="statu">
                    <el-radio-group v-model="editForm.statu">
                        <el-radio :label=0>禁用</el-radio>
                        <el-radio :label=1>正常</el-radio>
                    </el-radio-group>
                </el-form-item>
                <el-form-item label="排序号" prop="orderNum">
                    <el-input v-model="editForm.orderNum" :min="1" label="排序号">1</el-input>
                </el-form-item>

                <el-form-item>
                    <el-button type="primary" @click="submitForm('editForm')">立即创建</el-button>
                    <el-button @click="resetForm('editForm')">重置</el-button>
                </el-form-item>
            </el-form>
        </el-dialog>
    </div>
</template>

<script>
    import request from "../../utils/request";

    export default {
        name: "Menu",
        created() {
            this.getMenuTree()
        },
        methods:{

            handleClose(){
                this.dialogFormVisible = false
            },

            getMenuTree(){
                // request.get("/user/sys/menus/list").then(res =>{
                //     alert("meuns:"+res.data);
                //     //进行弹窗提示
                //     if (res.code == '0'){
                //         this.tableData = res.data
                //
                //     }else {
                //         this.$message({
                //             type:"error",
                //             message:res.msg
                //         })
                //     }
                //
                // })
            },

            addDialogForm(){
                console.log("来了")
                this.dialogFormVisible=true

            },

            handleClose(){

            },
            handleDelete(id){
                console.log(id)
                request.delete("/user/" + id).then(res =>{
                    //进行弹窗提示
                    if (res.code == '0'){
                        this.$message({
                            type:"success",
                            message:"删除成功"
                        })

                        this.load()//重新加载

                    }else {
                        this.$message({
                            type:"error",
                            message:res.msg
                        })
                    }

                })
            },

        },
        data(){
            //默认不显示
            return {
                dialogFormVisible:false,
                editForm:{

                },
                editFormRules:{
                    parentId:[
                        {required:true,message:'请选择上级菜单',trigger:'blur'}
                    ],
                    name:[
                        {required:true,message:'请输入名称',trigger:'blur'}
                    ],
                    perms:[
                        {required:true,message:'请输入权限编码',trigger:'blur'}
                    ],
                    type:[
                        {required:true,message:'请选择状态',trigger:'blur'}
                    ],
                    orderNum:[
                        {required:true,message:'请输入排序号',trigger:'blur'}
                    ],
                    statu:[
                        {required:true,message:'请选择状态',trigger:'blur'}
                    ]
                },
                tableData: [{
                    id: 1,
                    date: '2016-05-02',
                    name: '王小虎',
                    address: '上海市普陀区金沙江路 1518 弄',
                    statu: 1,
                    type:0
                }, {
                    id: 2,
                    date: '2016-05-04',
                    name: '王小虎',
                    address: '上海市普陀区金沙江路 1517 弄',
                    statu: 0,
                    type:1
                }, {
                    id: 3,
                    date: '2016-05-01',
                    name: '王小虎',
                    address: '上海市普陀区金沙江路 1519 弄',
                    type:2,
                    children: [{
                        id: 31,
                        date: '2016-05-01',
                        name: '王小明',
                        address: '上海市普陀区金沙江路 1519 弄',
                        type:1
                    }, {
                        id: 32,
                        date: '2016-05-01',
                        name: '王小真',
                        address: '上海市普陀区金沙江路 1519 弄',
                        type:2
                    }]
                }]
            }
        }
    }
</script>

<style scoped>

</style>